package com.example.yin.service.impl;


import com.example.yin.dao.RankSongMapper;

import com.example.yin.domain.RankSong;
import com.example.yin.service.RankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RankServiceImpl implements RankService {

    @Autowired
    private RankSongMapper rankMapper;


    @Override
    public boolean addRank(RankSong rank) {

        return rankMapper.insertSelective(rank) > 0 ? true:false;
    }

    @Override
    public double rankOfSongId(Integer songId) {
        List<RankSong> sranks = rankMapper.selectScoreBySongId(songId);
        if(sranks.size() == 0)
            return -1;
        double sum = 0;
        for(int i = 0; i < sranks.size(); i++){
            sum += (double)sranks.get(i).getScore();
        }
        return sum / sranks.size();
    }

    @Override
    public double getMyRank(int userId, int songId) {
        RankSong  myrank = rankMapper.selectScoreBySongIdAndUserId(userId, songId);
        if(myrank != null)
            return myrank.getScore();
        else
            return -1;
    }
}
